<?php
defined('IN_APPS') or die('No direct access!');

/**
 * User model, Any operation regarding users will be handled here.
 */  
class UserModel extends Model {
	
	// must must be same with database table name
	protected $name = 'users';
	
	public function getUser($username)
	{
		$sql = "SELECT * FROM " . $this->tableName . " WHERE username = '" . $username . "'";
		
		// run query
		$query = mysql_query($sql);
		
		$result = null;
		
		if ($query) {
			$result = mysql_fetch_array($query);
		}
		else {
			die('No user!');
		}
		
		return $result;
	}
	
	
	
	/**
	 * Check if user is Admin
	 * 
	 * @return boolean	True if admin, false if registered
	 */
	public function isAdmin()
	{
		$sql = 'SELECT * FROM users WHERE id = ' . $_SESSION['user_id'];
			
		$query = mysql_query($sql);

		if ($query) {
			$row = mysql_fetch_array($query);

			// id 1 == admin
			if ($row['group_id'] == 1) {
				return true;
			}
			else {
				return false;
			}
		}
	}
	
	
	/**
	 * Get user info
	 */
	public function getProfile()
	{
		$sql = 'SELECT * FROM ' . $this->tableName . ' WHERE id = ' . $_SESSION['user_id'];
		
		// start query database
		$query = mysql_query($sql);
		
		// checking
		if ($query) {
			
			// success
			$result = mysql_fetch_array($query);
			
			// return to controller
			return $result;
		}
		else {
			
			// failed
			die('Error: ' . mysql_error());
			
		}
	}
}